'use strict';
const {getDefaultModelConfig} = require("../../utils/common");

module.exports = app => {
    const { INTEGER, TINYINT, TEXT } = app.Sequelize;
    const UserPost =app.model.define(
        'UserPost',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            user_id: INTEGER,
            content: TEXT,
            images: TEXT, // 图片链接，逗号拼接
            like_count: {type: INTEGER, defaultValue: 0}, // 点赞数量
            status: {type: TINYINT, defaultValue: 0}, // 1:正常 0:已删除
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'user_post',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    UserPost.associate = function() {
        app.model.Business.UserPost.belongsTo(app.model.Business.User, {as: 'user', foreignKey: 'user_id'});
        app.model.Business.UserPost.hasMany(app.model.Business.UserPostLike, {as: 'likes', foreignKey: 'post_id'});
    }

    return UserPost;
};
